package cn.supermarket.controller;

import cn.supermarket.ex.ServiceException;
import cn.supermarket.pojo.dto.AdminAddNewDTO;
import cn.supermarket.pojo.dto.AdminLoginDTO;
import cn.supermarket.pojo.dto.ChangePasswordDTO;
import cn.supermarket.pojo.dto.RoleAddDTO;
import cn.supermarket.pojo.vo.AdminDetailsVO;
import cn.supermarket.service.iservice.IAdminService;
import cn.supermarket.web.JsonResult;
import cn.supermarket.web.State;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
//
@Api(tags = "管理员")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;


    @PostMapping("/add-admin")
    public JsonResult<Void> addNewAdmin(AdminAddNewDTO adminAddNewDTO) {
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/update-isEnable")
    public JsonResult<Void> updateEnableById(Long id){
        adminService.updateEnableById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update-information")
    public JsonResult<Void> updateInformationById(AdminDetailsVO adminDetailsVO){
        adminService.updateInformationById(adminDetailsVO);
        return JsonResult.ok();
    }

    @PostMapping("/delete-admin")
    public JsonResult<Void> deleteAdminById(Long id){
        adminService.deleteAdminById(id);
        return JsonResult.ok();
    }

    @GetMapping("/select-admins")
    public JsonResult<List<AdminDetailsVO>> getAdminList(){
        List<AdminDetailsVO> list = adminService.list();
        return JsonResult.ok(list);
    }

    @PostMapping("/insert-role")
    public JsonResult<Void> addRole(RoleAddDTO roleAddDTO){
        adminService.addRole(roleAddDTO);
        return JsonResult.ok();
    }

    @ApiOperation("添加权限")
    @PostMapping("/insert-permission")
    public JsonResult<Void> addPermission(String roleName,String[] permissionNames){
        adminService.addPermission(roleName,permissionNames);
        return JsonResult.ok();
    }

    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO) {
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }


    @PostMapping("/update-password")
    public JsonResult<Void> renewPassword(ChangePasswordDTO changePasswordDTO){
        adminService.changePassword(changePasswordDTO);
        return JsonResult.ok();
    }



}
